<template>
	<view class="content">
		<view class="menu-block">
			<view class="form-item" v-if="commentType===1">
				<view style="display: flex;flex-direction: row;">
					<view class="must" style="color: #EA0000;">*</view>
					<view class="form-label">评分</view>
				</view>
				<view class="score" style="margin: 40rpx 0;">
					<uni-rate v-model="form.rateValue" @change="onChange" :readonly="false" active-color="red"
						:size="22" />
				</view>
			</view>
			<view class="form-item" style="border-bottom: none;">
				<view style="display: flex;flex-direction: row;">
					<view class="must" style="color: #EA0000;">*</view>
					<view class="form-label">评论</view>
				</view>
				<view class="form-content" style="margin: 40rpx 0;">
					<textarea v-model="form.content" placeholder="请简述你的评价" class="info-crl" maxlength="300"></textarea>
					<view class="iconfont icon-gengduo1 arrow"></view>
				</view>
			</view>
			<view class="card-item" v-if="commentType===1">
				
				<view class="title FZHZGBJ">上传图片（最多6张）</view>
			
				<view class="upload">
					<view v-for="(item,index) in form.url" class="image-item">
						<image mode="aspectFit" :src="cloudStorage+item" class="img">
						</image>
						<view @click="closeImg(index,item)" style="display: inline;">
							<view class="iconfont">
								<view class="iconfont icon-a-guanbi"></view>
							</view>
						</view>
					</view>
					<view class="upload-content" v-if="form.url.length<6" @click="chooseImage">
						<image
							src="https://suzhou-charge-pile-1306862033.cos.ap-chengdu.myqcloud.com/uniapp//images/upload.png"
							class="upload"></image>
					</view>
				</view>
			</view>
		</view>
		<view class="step" @click="handleSave">
			提交
		</view>
	</view>
</template>

<script>
	import {
		userAddComment,
		commentConsumptionOrder
	} from '@/api/acticity.js';
	import {
		uploadFile
	} from '@/api/file.js'
	import {
		validate
	} from '@/util/validate.js'

	export default {
		data() {
			return {
				form: {
					score: '',
					content: '',
					orderId: '',
					url: [],
				},
				type: 1,
				commentType: 1,
				rules: {
					score: {
						name: '评分',
						required: true
					},
					content: {
						name: '你的评价',
						required: true
					},
				},
				id: null,
			}
		},
		methods: {
			// 上传视频和图片
			chooseImage(e) {
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					sourceType: ['album', 'camera'],
					success: (res) => {
						let filePath = res.tempFilePaths[0];
						uni.showLoading({
							title: '上传中',
							mask: true
						})
						uploadFile(filePath).then(res => {
							this.form.url.push(res.url)
						})
					}
				})
			},
			// 删除图片
			closeImg(index, url) {
				var that = this;
				uni.showModal({
					title: '提示',
					content: '是否删除当前文件',
					success: function(res) {
						if (res.confirm) {
							that.form.url.splice(index, 1);
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			onChange(e) {
				this.form.score = e.value
			},
			handleSave() {
				console.log(this.commentType)
				if (this.commentType == 2) {
				
					if (this.form.content == '') {
						this.showToast('请填写评论内容');
						return;
					}
					commentConsumptionOrder({
						id: this.form.orderId,
						commentstr: this.form.content
					}).then(res => {
						this.showToast(res, () => {
							uni.redirectTo({
								url: '/page_other/merchant_activity/index'
							});
						})
					})
				} else {
					let form = JSON.parse(JSON.stringify(this.form));
					if (validate(form, this.rules)) {
						uni.showLoading({
							title: '加载中',
							mask: true
						})
						userAddComment(form).then((res) => {
							uni.hideLoading()
							this.showToast(res, () => {
								uni.redirectTo({
									url: '/page_other/merchant_activity/index?status=2'
								});
							})
						}).catch((res) => {
							uni.hideLoading()
							this.showToast('提交失败');
						})
					}

				}
			}

		},
		onLoad(e) {
			this.form.orderId = e.id
			this.commentType = e.commentType
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #fafafa;
	}

	.content {
		width: 100%;
		padding: 30rpx;

		.step {
			width: 70%;
			height: 100rpx;
			display: flex;
			background: linear-gradient(90deg, #FF2F34 0%, #FF6B18 100%);
			color: #fff;
			margin: 30rpx auto;
			border-radius: 60rpx;
			justify-content: center;
			align-items: center;
			position: fixed;
			bottom: 30rpx;
			right: 118rpx;
		}

		.card-item {
			.title-item {
				display: flex;
				align-items: center;

				.must {
					color: #E2373C;
					font-weight: bold;
				}

				.title {
					color: #1A1A1A;
					font-size: 30rpx;
					line-height: 70rpx;
				}

			}

			.title {
				color: #1A1A1A;
				font-size: 30rpx;
				line-height: 70rpx;
			}

			.rate-item {
				width: 100%;
				display: flex;
				align-items: center;
				margin-bottom: 20rpx;

				.name {
					font-size: 28rpx;
					width: 200rpx;
					margin-right: 20rpx;
					color: #4c4a4a;
				}

				.rate {
					width: calc(100% - 220rpx);
				}
			}

			.card-input {
				border-bottom: 1px solid #f7f7f7;
				font-size: 28rpx;
				height: 240rpx;
				padding-bottom: 20rpx;
			}

			.upload {
				display: flex;
				flex-direction: row;
				flex-wrap: wrap;

				.image-item {
					margin-top: 20rpx;
					margin-right: 20rpx;
					display: flex;
					flex-direction: row;
					// box-sizing: border-box;
					// border: 1px solid #efefef;

					.img {
						width: 190rpx;
						height: 200rpx;
						border-radius: 20rpx;
					}

					.iconfont {
						.iconfont {
							font-size: 30rpx;
							position: absolute;
							margin-left: -30rpx;
						}
					}
				}

				.image-item:nth-child(3n) {
					margin-right: 0;
				}

				.delete-item {
					width: 100%;
					display: flex;
					justify-content: flex-end;

					.delete {
						width: 20px;
						height: 20px;
						position: absolute;
					}
				}

				.image-item1 {
					display: flex;
					flex-direction: row;

					.delete {
						margin-top: 10rpx;
						margin-left: -40rpx;
						width: 20px;
						height: 20px;
						position: absolute;
					}
				}

				.upload-content {
					position: relative;
					background: #fafafa;
					border-radius: 5px;
					width: 200rpx;
					height: 200rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					margin-top: 20rpx;

					.title {
						position: absolute;
						top: 50%;
						left: 50%;
					}

					.upload {
						width: 90rpx;
						height: 90rpx;
					}
				}
			}

		}

		.menu-block {
			margin-top: 30rpx;
			background: #fff;
			border-radius: 12px;
			padding: 30rpx;

			.form-item {
				display: flex;
				flex-direction: column;
				border-bottom: 1px solid #efefef;
				padding-top: 30rpx;

				.form-label {
					font-size: 32rpx;
					color: #1A1A1A;
				}

				.form-content {
					display: flex;
					flex-direction: column;
					// align-items: center;

					.arrow {
						flex-grow: 0;
						color: #767676;
						font-size: 32rpx;
					}
				}
			}
		}
	}
</style>